package com.dim.list_orders;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class items extends Activity {

	private SQLiteDatabase database;
	private ListView listView;
	private SimpleCursorAdapter scAdapter;
	private SimpleCursorAdapter sc_t;
	private Cursor cursor;

	private Spinner s_type;
	private Cursor cursor_t;
	private int id_it_t;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(com.dim.list_orders.R.layout.orders);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				com.dim.list_orders.R.layout.title_add);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(com.dim.list_orders.R.string.l_items);

		listView = (ListView) findViewById(R.id.list);
		s_type = (Spinner) findViewById(com.dim.list_orders.R.id.s_categoryes);
		class_sqlite dbOpenHelper = new class_sqlite(this,
				getString(com.dim.list_orders.R.string.db_name));
		database = dbOpenHelper.openDataBase();

		load_types();

		get_cursor_all_items();
		scAdapter = new SimpleCursorAdapter(this,
				com.dim.list_orders.R.layout.row_list, cursor, new String[] {
						"name", "number" },
				new int[] { R.id.text1, R.id.text2 });
		listView.setAdapter(scAdapter);
		registerForContextMenu(listView);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(getBaseContext(),
						orders_list_users_per_items.class);
				i.putExtra("id_it", cursor.getInt(cursor.getColumnIndex("_id")));
				i.putExtra("name",
						cursor.getString(cursor.getColumnIndex("name")));
				startActivity(i);
			}
		});

		s_type.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				reload();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	private void load_types() {
		stopManagingCursor(cursor_t);
		cursor_t = database.query("items_type", new String[] { "_id", "name" },
				null, null, null, null, "name");
		startManagingCursor(cursor_t);
		sc_t = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cursor_t,
				new String[] { "name" }, new int[] { android.R.id.text1 });
		sc_t.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s_type.setAdapter(sc_t);
	}

	private void get_cursor_all_items() {
		stopManagingCursor(cursor);
		id_it_t = cursor_t.getInt(cursor_t.getColumnIndex("_id"));
		cursor = database
				.rawQuery(

						"SELECT items._id, items.name, ord.number from items left join items_type on items.id_it_t=items_type._id left join (select sum(number) as number, id_it from orders left join status on orders.id_st=status._id where status.show=1 group by orders.id_it) as ord on items._id=ord.id_it where items_type.show=1 and items.id_it_t="
								+ id_it_t + " order by items.name asc", null);

		startManagingCursor(cursor);
	}

	private void reload() {
		get_cursor_all_items();
		scAdapter.changeCursor(cursor);
	}

	public void b_add(View v) {
		Intent i = new Intent(getBaseContext(), items_manager.class);
		i.putExtra("id_it", 0);
		startActivity(i);
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.list) {
			menu.setHeaderTitle(com.dim.list_orders.R.string.menu_management);
			MenuInflater inflater = getMenuInflater();
			inflater.inflate(com.dim.list_orders.R.menu.menu_edit_delete, menu);
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, 0, Menu.NONE, com.dim.list_orders.R.string.m_add)
				.setIcon(android.R.drawable.ic_menu_add);
		menu.add(Menu.NONE, 1, Menu.NONE,
				com.dim.list_orders.R.string.m_refresh).setIcon(
				android.R.drawable.ic_menu_revert);
		menu.add(Menu.NONE, 2, Menu.NONE, com.dim.list_orders.R.string.m_filtr)
				.setIcon(android.R.drawable.ic_menu_crop);
		menu.add(Menu.NONE, 4, Menu.NONE,
				com.dim.list_orders.R.string.m_preference).setIcon(
				android.R.drawable.ic_menu_preferences);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			b_add(listView);
			break;
		case 1:
			reload();
			break;
		case 2:
			Intent i = new Intent(this, filtr.class);
			i.putExtra("table", "items_type");
			startActivity(i);
			break;
		case 4:
			Intent a = new Intent(this, preferences.class);
			startActivity(a);
			break;
		default:
			break;
		}

		return false;
	}

	private void delete(final long id) {
		new AlertDialog.Builder(this)
				.setTitle(
						getString(com.dim.list_orders.R.string.d_delete_title))
				.setMessage(
						getString(com.dim.list_orders.R.string.d_delete_text))
				.setNegativeButton(android.R.string.no, null)
				.setPositiveButton(android.R.string.yes, new OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
						String[] args = { String.valueOf(id) };
						database.delete("items", "_id=?", args);
						reload();
					}
				}).create().show();
	}

	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case com.dim.list_orders.R.id.b_edit:
			// alert_input_dialog(R.string.d_edit_items_title,
			// R.string.d_edit_items_text,
			// cursor.getString(cursor.getColumnIndex("name")));
			// id_edit = info.id;

			Intent i = new Intent(getBaseContext(), items_manager.class);
			i.putExtra("id_it", info.id);
			i.putExtra("id_it_t", cursor.getInt(cursor.getColumnIndex("_id")));
			i.putExtra("name_it",
					cursor.getString(cursor.getColumnIndex("name")));

			startActivity(i);

			// reload();
			break;
		case com.dim.list_orders.R.id.b_delete:
			delete(info.id);
			break;
		}
		return true;
	}

	protected void onDestroy() {
		super.onDestroy();
		database.close();
	}

}
